package com.example.tianyutong.qanda.ui.question;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.tianyutong.qanda.R;
import com.example.tianyutong.qanda.base.BaseActivity;
import com.example.tianyutong.qanda.bean.wanandroid.BaseResultBean;
import com.example.tianyutong.qanda.bean.wanandroid.LoginBean;
import com.example.tianyutong.qanda.data.UserUtil;
import com.example.tianyutong.qanda.data.room.Injection;
import com.example.tianyutong.qanda.http.HttpClient;
import com.example.tianyutong.qanda.utils.ToastUtil;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Date 2020/4/17 20:11
 * @Desc 发布问题
 */

public class AddQuestionActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private EditText et_title;
    private EditText et_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_question);
        initView();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.title_tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        et_title = (EditText) findViewById(R.id.et_title);
        et_content = (EditText) findViewById(R.id.et_content);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 发布问题
     *
     * @param view
     */
    public void submit(View view) {
        String title = et_title.getText().toString().trim();
        if (TextUtils.isEmpty(title)) {
            Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String content = et_content.getText().toString();
        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "问题描述不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        HttpClient.Builder.getQAndAServer().addQuestion(title, content)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResultBean bean) {
                        if (bean.getErrorCode() == 0) {
                            Toast.makeText(AddQuestionActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(AddQuestionActivity.this, bean.getErrorMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(AddQuestionActivity.this, "发布失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
